<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" v-show="showSearch">
      <el-row :gutter="10">
        <el-col :span="4">
          <el-form-item prop="dictName">
            <el-input
              v-model="queryParams.modelName"
              placeholder="请输入模型名称"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="typeList" @selection-change="handleSelectionChange" v-table height="0">
      <el-table-column label="序号" type="index" width="50" align="center">
        <template v-slot="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="模型描述" align="center" prop="description" :show-overflow-tooltip="true" />
      <el-table-column label="模型名称" align="center" prop="name" :show-overflow-tooltip="true" />
      <el-table-column label="模型作者" align="center" prop="author"  />
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body center :close-on-click-modal="false">
      <el-form ref="form" :model="form" label-width="80px">

        <el-form-item label="模型名称" prop="name">
          <el-input v-model="form.name" readOnly />
        </el-form-item>
        <el-form-item label="模型描述" prop="description">
          <el-input v-model="form.description" readOnly />
        </el-form-item>
        <el-form-item label="api名称" prop="status">
          <el-input v-model="form.description" readOnly/>
        </el-form-item>
        <el-form-item
          label="api描述"
          prop="image"
        >
          <el-input v-model="form.description" readOnly/>
        </el-form-item>

        <el-form-item
          label="附件"
          prop="attachment"
        >
          <el-upload
            ref="upload"
            :limit="5"
            :action="upload.url"
            :headers="upload.headers"
            :file-list="upload.fileList"
            :on-progress="handleFileUploadProgress"
            :on-success="handleFileSuccess"
            :on-remove="handleRemove"
            :auto-upload="true"
            :before-upload="beforeUpload"
          >
          </el-upload>

        </el-form-item>

        <el-form-item label="内容">
          <editor :read-only="true"
            v-model="form.richText"
            :min-height="192"
          />
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" readOnly></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="open = false;this.reset" icon="el-icon-check">确 定</el-button>

      </div>
    </el-dialog>
  </div>
</template>

<script>

import {listBasicInfoCollect,getBasicInfo,delBasicInfo,addBasicInfo,updateBasicInfo,publishApply} from "@/api/model/info";
import {listCollect} from "@/api/model/collect";
import {getToken} from "@/utils/auth";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {listCategory} from "@/api/model/category";

export default {
  name: "basicInfo",
  components: {Treeselect},
  dicts: ['sys_normal_disable'],
  data() {
    return {
      menuOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        modelName: '',
        status: undefined
      },
      // 表单参数
      form: {},
      upload: {
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址 /网关/控制器
        url: process.env.VUE_APP_BASE_API + "/file/upload",
        fileList: [],
      },



    };
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  methods: {
    getList() {
      this.loading = true;
      listCollect(this.queryParams).then(response => {
          this.typeList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        description: undefined,
        status: "0",
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.modelName = '';
      this.handleQuery();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dictId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBasicInfo(id).then(response => {
        this.form = response.data;
        const annex = this.form['annexUrl'];
        this.upload.fileList = JSON.parse(annex);
        this.open = true;
        this.title = "查看模型基本信息";
      });
    },


    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.categoryName,
        children: node.children
      };
    },
    /** 查询菜单下拉树结构 */
    getTreeselect() {
      listCategory().then(response => {
        this.menuOptions = [];
        const menu = { id: 0, categoryName: '顶级节点', children: [] };
        menu.children = this.handleTree(response.data, "id","parentId");
        this.menuOptions.push(menu);
      });
    },
  }
};
</script>

<style scoped>
.item {
  margin-top: 10px;
  margin-right: 40px;
}
</style>
